package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Organization;
import com.woniu.service.IServiceDisplayService;
import com.woniu.service.IServicesService;
import com.woniu.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务展示表 前端控制器
 * </p>
 *
 * @author xiaotang
 * @since 2022-10-18
 */
@RestController
@RequestMapping("/serviceDisplay")
@Api(tags = "服务管理展示")
public class ServiceDisplayController extends BaseController{
    @Autowired
    private IServiceDisplayService serviceDisplayService;


    @ApiOperation("根据服务id查询服务信息")
    @GetMapping("/{servicesId}")
    public Result queryById(@PathVariable Long servicesId) throws ParseException {
        QueryServiceDetailVo queryServiceDetailVo = serviceDisplayService.queryById(servicesId);
        return toDataResult(queryServiceDetailVo);
    }

    @ApiOperation("分页搜索")
    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result search(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody QueryServiceDisplayVo queryServiceDisplayVo) throws ParseException {
        PageResult pageResult = serviceDisplayService.search(currentPage, pageSize, queryServiceDisplayVo);
        return toDataResult(pageResult);
    }

    @ApiOperation("新增服务")
    @PostMapping
    public Result add(@RequestBody ServiceSubmitVo serviceSubmitVo){
        Boolean flag = serviceDisplayService.add(serviceSubmitVo);
        return toResult(flag);
    }

    @ApiOperation("修改服务")
    @PutMapping
    public Result modify(@RequestBody ServiceSubmitVo serviceSubmitVo){
        Boolean flag = serviceDisplayService.modify(serviceSubmitVo);
        return toResult(flag);
    }

    @ApiOperation("导出服务数据")
    @PostMapping("/export")
    public void download(HttpServletResponse response, @RequestBody QueryServiceDisplayVo queryServiceDisplayVo) throws IOException, ParseException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        PageResult pageResult = serviceDisplayService.search(1, 100, queryServiceDisplayVo);
        List<QueryServiceDetailVo> queryServiceDetailVoList = (List<QueryServiceDetailVo>) pageResult.getData();
        List<ExportServiceVo> exportServiceVoList = new ArrayList<>();
        for (QueryServiceDetailVo queryServiceDetailVo : queryServiceDetailVoList) {
            ExportServiceVo exportServiceVo = new ExportServiceVo();
            BeanUtils.copyProperties(queryServiceDetailVo, exportServiceVo);
            exportServiceVoList.add(exportServiceVo);
        }


        EasyExcel.write(response.getOutputStream(), ExportServiceVo.class).sheet("模板").doWrite(exportServiceVoList);
    }
}
